package com.yesido.javabase.concurrent.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author yuliang
 * @description 阿里推荐的线程池
 * @date 2021/6/20 17:17
 */
public class AlbbThreadPool {
    static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("demo-pool-%d").build();

    /**
     * Common Thread Pool
     */
    static ExecutorService pool = new ThreadPoolExecutor(30, 200,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(10240), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        // list拆分
        for (int i = 0; i <10000 ; i++) {
            list.add("yuliang---"+i);
        }

        long start = System.currentTimeMillis();
            for (String st : list) {
                pool.execute(new Task1(st));
            }

        pool.shutdown();
//        Thread thread = new Thread();
//        for (String s : list) {
//            thread = new Thread(new Task1(s));
//            thread.start();
//            try {
//                thread.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        System.out.println("耗时：--->" + (System.currentTimeMillis() - start) + "ms");
    }
}
